#include "aliyun_model.h"
#include "in_out.h"
#include "fingerprint.h"
#include "application.h"
#include "jansson.h"
#include "mqtt.h"
#include "eeprom.h"

AM_DOUT_MAPPING dout_map[DOUT_MAX_CH] = 
{
	{0,STRI(DOUT1_NAME)},
	{0,STRI(DOUT2_NAME)},
	{0,STRI(DOUT3_NAME)},
	{0,STRI(DOUT4_NAME)},
	{0,STRI(DOUT5_NAME)},
	{0,STRI(DOUT6_NAME)},
};

AM_DIN_MAPPING din_map[DIN_MAX_CH] = 
{
	{0,STRI(DIN1_NAME)},
	{0,STRI(DIN2_NAME)},
	{0,STRI(DIN3_NAME)},
	{0,STRI(DIN4_NAME)},
	{0,STRI(DIN5_NAME)},
	{0,STRI(DIN6_NAME)},
};

AM_TEXT_MAPPING statues_text = {STRI(STATUS_NAME),{0}};


AM_FUNC_SERVER_MAPPING server_funcs[SERVER_FUNCTION_ITEM] = 
{
	{STRI(SERVER_FUNCTION1),SERVER_FUNCTION1},
	{STRI(SERVER_FUNCTION2),SERVER_FUNCTION2},
	{STRI(SERVER_FUNCTION3),SERVER_FUNCTION3},
	{STRI(SERVER_FUNCTION4),SERVER_FUNCTION4},
	{STRI(SERVER_FUNCTION5),SERVER_FUNCTION5},
};

AM_GEO_LOCATION geo_location;

void AM_Init(void)
{
	
}

void AM_BuildStatuesText(char * text,u8 * result_buff)
{
	char json_str[128];
	sprintf(json_str,"{\"params\": {\"%s\": \"%s\"}}",STRI(STATUS_NAME),text);
	memcpy(result_buff,json_str,strlen(json_str));
}

void AM_RefreshOutputChannel(void)
{
	for(int i=0;i<DOUT_MAX_CH;i++)
	{
		u8 state = INOUT_ReadOut(i + 1);
		if(dout_map[i].val != state)
		{
			INOUT_SetOutCh(i + 1,dout_map[i].val);
		}
	}
}

void AM_RefreshOutputCH(u8 ch)
{
//	u8 state = INOUT_ReadOut(ch);
//	if(dout_map[ch - 1].val != state)
	{
		INOUT_SetOutCh(ch,dout_map[ch - 1].val);
	}
}


//RegisterFingerprint
void SERVER_FUNCTION1(void)
{
	printf("call %s \r\n",STRI(SERVER_FUNCTION3));
	app_flags.register_fingerprint_f = 1;
}
//RemoveAllFingerprint
void SERVER_FUNCTION2(void)
{
	printf("call %s \r\n",STRI(SERVER_FUNCTION3));
	app_flags.remover_fingerprint_f = 1;
}
//IdentifyFingerprint
void SERVER_FUNCTION3(void)
{
	printf("call %s \r\n",STRI(SERVER_FUNCTION3));
	app_flags.identify_fingerprint_f = 1;
}

void SERVER_FUNCTION4(void)
{
	printf("call %s \r\n",STRI(SERVER_FUNCTION4));
	char * json_buff = (char *)&MQTT_CMDOutPtr[2];
	
	char params_str[32];
	sscanf(json_buff,"%*[^,],%*[^,],%[^,]",params_str);
	u32 temp_hold_time;
	sscanf(params_str,"%*[^:]:%*[^:]:%d",&temp_hold_time);
	sys.hold_time = temp_hold_time;
	sys.info.hold_time = sys.hold_time;
	FLASHEx_EEPROM_WRITE(0,(u8 *)&sys.info,sizeof(APP_INFO));
	printf("hold_time=%d\r\n",sys.hold_time);
	app_flags.set_hold_time_f = 1;
}

void SERVER_FUNCTION5(void)
{
	printf("call %s \r\n",STRI(SERVER_FUNCTION5));
}

//获取位置的 JSON 字符串
//void GET_LOCATION_JSON(GEO_LOCATION)(char * buf)
void GET_LOCATION_JSON_FUN(char * buf)
{
	char temp_buf[256];
	char * format_str = "\"%s\":{\"%s\":%d,\"%s\":%lf,\"%s\":%lf,\"%s\":%lf}";
	sprintf(temp_buf,format_str,
					STRI(GEO_LOCATION),
					STRI(GEO_LOCATION_COORDINATESYSTEM),geo_location.CoordinateSystem,
					STRI(GEO_LOCATION_LATITUDE),geo_location.Latitude,
					STRI(GET_LOCATION_LONGITUDE),geo_location.Longitude,
					STRI(GET_LOCATION_ALTITUDE),geo_location.Altitude);
	memcpy(buf,temp_buf,strlen(temp_buf));
	
}